package com.boyon.customer.action;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boyon.common.context.ContextHolder;
import com.boyon.common.tools.ObjectTool;
import com.boyon.customer.domain.CustomerData;
import com.boyon.customer.service.customer.CustomerServiceInterface;
import com.boyon.hbm.criteria.Query;
import com.boyon.web.param.annotation.ValidatorRequestBody;
import com.boyon.web.response.AbstractResponse;
import com.boyon.web.response.PageResponse;
import com.boyon.web.response.SuccessResponse;

/**
 * ClassName:CustomerAction <br/>
 * Function: 客户档案. <br/>
 * Date: 2018年4月2日 下午10:50:10 <br/>
 * 
 * @author sunshulin
 * @version
 * @since JDK 1.8
 * @see
 */
@Controller
@RequestMapping(value = "/boyon/customer", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class CustomerAction {

	private Map<Integer, CustomerServiceInterface> interfaces = null;

	/**
	 * 
	 * query:客户档案列表查询. <br/>
	 * 
	 * @author sunshulin
	 * @param account
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/{type}/query")
	@ResponseBody
	public PageResponse query(@PathVariable(name = "type") Integer type,
			@ValidatorRequestBody CustomerData customerData) {
		CustomerServiceInterface service = getCustomerServiceInterface(type);
		Query query = new Query(customerData.page());
		return new PageResponse(service.query(customerData, query),
				query.getTotal());
	}

	/**
	 * 
	 * create:编辑客户档案. <br/>
	 * 
	 * @author sunshulin
	 * @param type
	 * @param customerData
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/{type}/editor")
	@ResponseBody
	public AbstractResponse editor(@PathVariable(name = "type") Integer type,
			@ValidatorRequestBody CustomerData customerData) {
		CustomerServiceInterface service = getCustomerServiceInterface(type);
		if (ObjectTool.isNotNull(customerData.getCustomerId())) {
			service.editor(customerData);
		} else {
			service.save(customerData);
		}
		return new SuccessResponse();
	}

	/**
	 * 
	 * delete:删除客户档案. <br/>
	 * 
	 * @author sunshulin
	 * @param type
	 * @param customerData
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/{type}/delete")
	@ResponseBody
	public AbstractResponse delete(@PathVariable(name = "type") Integer type,
			@ValidatorRequestBody CustomerData customerData) {
		CustomerServiceInterface service = getCustomerServiceInterface(type);
		service.delete(customerData.getCustomerId());
		return new SuccessResponse();
	}

	/**
	 * 
	 * find:查找客户档案. <br/>
	 * 
	 * @author sunshulin
	 * @param type
	 * @param customerData
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/{type}/find")
	@ResponseBody
	public AbstractResponse find(@PathVariable(name = "type") Integer type,
			@ValidatorRequestBody CustomerData customerData) {
		CustomerServiceInterface service = getCustomerServiceInterface(type);
		return new SuccessResponse(
				service.findByCustomerId(customerData.getCustomerId()));
	}

	/**
	 * 
	 * find:查找客户档案. <br/>
	 * 
	 * @author sunshulin
	 * @param type
	 * @param customerData
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/{type}/reset/pass")
	@ResponseBody
	public AbstractResponse resetPass(@PathVariable(name = "type") Integer type,
			@ValidatorRequestBody CustomerData customerData) {
		CustomerServiceInterface service = getCustomerServiceInterface(type);
		service.resetPass(customerData.getCustomerId());
		return new SuccessResponse();
	}

	/**
	 * 
	 * getCustomerServiceInterface:获取实现类. <br/>
	 * 
	 * @author sunshulin
	 * @param type
	 * @return
	 * @since JDK 1.8
	 */
	public CustomerServiceInterface getCustomerServiceInterface(Integer type) {
		if (interfaces == null) {
			interfaces = new HashMap<>();
			Set<CustomerServiceInterface> sets = ContextHolder
					.getBeanByClazz(CustomerServiceInterface.class);
			sets.stream().forEach(face -> {
				interfaces.put(face.getType(), face);
			});
		}
		return interfaces.get(type);
	}
}
